﻿using System;
using System.Linq;
using NPMonitor.Attributes;
using NPMonitor.DataModel;
using NPMonitor.Utility;
using OpenQA.Selenium.Remote;
using System.Threading;

namespace DSMWebshops
{
    public class RunJob : WebJobBase
    {
        protected override void ExecuteJob(HttpWatch.Plugin plugin)
        {
            WebDriverHelper.WebDriver = WebDriver;
            WebDriverHelper.DefaultTimeoutSecs = DefaultTimeoutSecs;

            Console.WriteLine("Starting Webshops Startscreen ");
            Startscreen(plugin);
            Console.WriteLine("Webshops Startscreen - " + " Finished");

            Console.WriteLine("Starting Webshops ClickNutritionalProductsEurope ");
            ClickNutritionalProductsEurope(plugin);
            Console.WriteLine("Webshops ClickNutritionalProductsEurope - " + " Finished");

            Console.WriteLine("Starting Webshops Login ");
            Login(plugin);
            Console.WriteLine("Webshops Login - " + " Finished");

            Console.WriteLine("Starting Webshops ClickMyFavorites ");
            ClickMyFavorites(plugin);
            Console.WriteLine("Webshops ClickMyFavorites - " + " Finished");

            Console.WriteLine("Starting Webshops EnterQuantityShowPrice ");
            EnterQuantityShowPrice(plugin);
            Console.WriteLine("Webshops EnterQuantityShowPrice - " + " Finished");

            Console.WriteLine("Starting Webshops AddToCart ");
            AddToCart(plugin);
            Console.WriteLine("Webshops AddToCart - " + " Finished");

            Console.WriteLine("Starting Webshops ClickCheckout ");
            ClickCheckout(plugin);
            Console.WriteLine("Webshops ClickCheckout - " + " Finished");

            Console.WriteLine("Starting Webshops OrderCheckout ");
            OrderCheckout(plugin);
            Console.WriteLine("Webshops OrderCheckout - " + " Finished");

            Console.WriteLine("Starting Webshops ClickChange ");
            ClickChange(plugin);
            Console.WriteLine("Webshops ClickChange - " + " Finished");

            Console.WriteLine("Starting Webshops CancelOrderCheckout ");
            CancelOrderCheckout(plugin);
            Console.WriteLine("Webshops CancelOrderCheckout - " + " Finished");

            Console.WriteLine("Starting Webshops ViewCart ");
            ViewCart(plugin);
            Console.WriteLine("Webshops ViewCart - " + " Finished");

            Console.WriteLine("Starting Webshops EmptyCart ");
            EmptyCart(plugin);
            Console.WriteLine("Webshops EmptyCart - " + " Finished");

            Console.WriteLine("Starting Webshops SureEmptyCart ");
            SureEmptyCart(plugin);
            Console.WriteLine("Webshops SureEmptyCart - " + " Finished");

            Console.WriteLine("Starting Webshops Logout ");
            Logout(plugin);
            Console.WriteLine("Webshops Logout - " + " Finished");
        }

        [Scenario("Webshops_01", "Startscreen", "Startscreen", null)]
        public void Startscreen(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_01";
            WebDriver.Navigate().GoToUrl("https://webshop.dsm.com/wwwasera/html/");
            Control.WaitEx(plugin, -1, true, true, 0);
        }

        [Scenario("Webshops_02", "NutritionalProductsEurope", "NutritionalProductsEurope", null)]
        public void ClickNutritionalProductsEurope(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_02";
            //Click on DSM Nutritional Products Europe
            var tablesMain = WebDriverHelper.GetWebElementsByTagName(WebDriver, "table");
            var tablesOverview = WebDriverHelper.GetWebElementsByTagName(tablesMain[0], "table");
            var tablesLeft = WebDriverHelper.GetWebElementsByTagName(tablesOverview[0], "table");
            var trsLeft = WebDriverHelper.GetWebElementsByTagName(tablesLeft[0], "tr");
            var link = WebDriverHelper.GetByTagName(trsLeft[2], "a");
            link.Click();
            Control.WaitEx(plugin, -1, true, true, 0);
        }

        [Scenario("Webshops_03", "Login", "Login", null)]
        public void Login(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_03";

            //Click on DSM Nutritional Products Europe
            var selectLanguage = WebDriverHelper.GetByName(WebDriver, "locale");

            var optionsLanguage = WebDriverHelper.GetWebElementsByTagName(selectLanguage, "option");
            optionsLanguage[0].Click();

            var textFieldsContentLeft = WebDriverHelper.GetWebElementsByClassName(WebDriver, "TextFieldContentLeft");
            textFieldsContentLeft[0].Clear();
            textFieldsContentLeft[0].SendKeys("GL_Performance");
            textFieldsContentLeft[1].Clear();
            textFieldsContentLeft[1].SendKeys("Gltest");

            var tables = WebDriverHelper.GetWebElementsByClassName(WebDriver, "lightborderTable");
            var tableLogin = WebDriverHelper.GetByTagName(tables[0], "table");
            var trs = WebDriverHelper.GetWebElementsByTagName(tableLogin, "tr");
            var buttonLogin = WebDriverHelper.GetByTagName(trs[3], "input");
            buttonLogin.Click();

            //取消记住密码窗口
            try
            {
                Thread.Sleep(2000);
                var alert = WebDriver.SwitchTo().Alert();

                if (alert != null)
                {
                    ((RemoteWebDriver)WebDriver).ExecuteScript("window.confirm = function() {return false}");
                }
            }
            catch
            { }

            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_04", "MyFavorites", "MyFavorites", null)]
        public void ClickMyFavorites(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_04";
            //Click on Order Now / My Favorites
            var divShopping = WebDriverHelper.GetById(WebDriver, "Shopping");
            ((RemoteWebDriver)WebDriver).ExecuteScript("document.getElementById('Shopping').style.visibility='visible'");
            var linkMyFavorites = WebDriverHelper.GetByCssSelector(divShopping, "span a");
            linkMyFavorites.Click();

            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            //((RemoteWebDriver)WebDriver).ExecuteScript("document.getElementById('Shopping').style.visibility='hidden'");
        }

        [Scenario("Webshops_05", "EnterQuantityShowPrice", "EnterQuantityShowPrice", null)]
        public void EnterQuantityShowPrice(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_05";
            //Enter quantity and Show Price
            var textQuantity = WebDriverHelper.GetByName(WebDriver, "ACSfp!tf!ProxyVDOPageIterator^[Surrogated]^NumericFlexField5!n|FORMAT_DECIMAL|||!t!ACSit$ProxyVDOPageIterator!2");
            textQuantity.Clear();
            textQuantity.SendKeys("25");

            //plugin.NextPageComment = "Webshops_ShowPrice";
            //Enter quantity and Show Price
            var buttonShowPrice = WebDriverHelper.GetByName(WebDriver, "ACSac!GetPrice!all!ACSit$ProxyVDOPageIterator!2");
            buttonShowPrice.Click();

            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            var divsTip = WebDriverHelper.GetWebElementsByClassName(WebDriver, "AseraTableStyle");
        }

        [Scenario("Webshops_06", "AddToCart", "AddToCart", null)]
        public void AddToCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_06";
            //Push button Add to Cart
            var buttonAddCart = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!AddItemsToCart!all");
            buttonAddCart[0].Click();

            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_07", "ClickCheckout", "ClickCheckout", null)]
        public void ClickCheckout(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_07";
            //Mouse over on Cart, select Checkout
            var divCSummary = WebDriverHelper.GetById(WebDriver, "CSummary");
            ((RemoteWebDriver)WebDriver).ExecuteScript("document.getElementById('CSummary').style.visibility='visible'");

            var linkCheckout = WebDriverHelper.GetWebElementsByName(divCSummary, "ACSac!SummaryNewOrder!all");
            linkCheckout[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_08", "OrderCheckout", "OrderCheckout", null)]
        public void OrderCheckout(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_08";
            //Enter Requested Delivery date (possible to 1 year ahead as today)
            string dateTemp = DateTime.Now.AddYears(1).ToString("MM/dd/yyyy");
            var textRequestedDeliveryDate = WebDriverHelper.GetByName(WebDriver, "ACSfp!tf!HeaderObj^RAD!d|FORMAT_DEFAULTDATE|||!");
            textRequestedDeliveryDate.Clear();
            textRequestedDeliveryDate.SendKeys(dateTemp);

            //Enter Purchase Order: GL Monitoring
            var textPurchaseOrder = WebDriverHelper.GetByName(WebDriver, "ACSfp!tf!HeaderObj^PONumber!!");
            textPurchaseOrder.Clear();
            textPurchaseOrder.SendKeys("GL Monitoring");

            //Push button Proceed (on the right)
            var linkproceed = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!Submit!all");
            linkproceed[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_09", "ClickChange", "ClickChange", null)]
        public void ClickChange(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_09";
            //Push button Add to Cart
            var buttonAddCart = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!EditRequest!all");
            buttonAddCart[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_10", "CancelOrderCheckout", "CancelOrderCheckout", null)]
        public void CancelOrderCheckout(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_10";
            //Push button Cancel
            var buttonCancel = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!Cancel!all");
            buttonCancel[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_11", "ViewCart", "ViewCart", null)]
        public void ViewCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_11";
            //Mouse over on Cart, select Checkout
            var divCSummary = WebDriverHelper.GetById(WebDriver, "CSummary");
            ((RemoteWebDriver)WebDriver).ExecuteScript("document.getElementById('CSummary').style.visibility='visible'");

            var linkView = WebDriverHelper.GetByName(divCSummary, "ACSac!ViewCart!all");
            linkView.Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
            //((RemoteWebDriver)WebDriver).ExecuteScript("document.getElementById('CSummary').style.visibility='hidden'");
        }

        [Scenario("Webshops_12", "EmptyCart", "EmptyCart", null)]
        public void EmptyCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_12";
            //Push Empty Cart
            var linkEmpty = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!EmptyCart!all");
            linkEmpty[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_13", "SureEmptyCart", "SureEmptyCart", null)]
        public void SureEmptyCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_13";
            //Push Ok button
            var linkSureEmpty = WebDriverHelper.GetWebElementsByName(WebDriver, "ACSac!Ok!all");
            linkSureEmpty[0].Click();
            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }

        [Scenario("Webshops_14", "Logout", "Logout", null)]
        public void Logout(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Webshops_14";
            //Push Logout button
            //var linkLogout = WebDriverHelper.GetByLinkText(WebDriver, "Logout");
            //linkLogout.Click();

            var divNavMain = WebDriverHelper.GetById(WebDriver, "nav-main");
            var tdsNavMain = WebDriverHelper.GetWebElementsByTagName(divNavMain, "td");
            var linkLogout = WebDriverHelper.GetByTagName(tdsNavMain[5], "a");
            linkLogout.Click();

            Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
        }
    }
}
